//
//  CDYMacro.h
//  Charging
//
//  Created by Eric on 2019/7/2.
//

#ifndef CDYMacro_h
#define CDYMacro_h

#pragma mark - log

#ifdef DEBUG
#define DString [NSString stringWithFormat:@"%s", __FILE__].lastPathComponent
#define DLog(...) printf("\n************\n%s 第%d行: %s\n************\n", [DString UTF8String] ,__LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String]);
#else
#define DLog(...)
#endif


#pragma mark - System Singletons  单例
// 偏好设置单例
#define kUserDefaults [NSUserDefaults standardUserDefaults]

//通知单例
#define kNotificationCenter  [NSNotificationCenter defaultCenter]

// 文件管理单例
#define kFileManager [NSFileManager defaultManager]


#pragma mark - filepath 文件路径

//Library/Caches 文件路径
#define FilePath ([[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil])
//获取temp
#define kPathTemp NSTemporaryDirectory()
//获取沙盒 Document
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//获取沙盒 Cache
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]


#pragma mark - normal 默认


#define kAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)
#define kKeyWindow ((UIWindow *)[UIApplication sharedApplication].keyWindow)

//获取应用版本号
#define kBundleVersion ((NSString *)[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"])
// 获取bundle Id
#define kBundleId [[NSBundle mainBundle] bundleIdentifier]
// 获取系统版本号
#define kIOSVersion ([UIDevice currentDevice].systemVersion.floatValue)


#pragma mark - Load Image 加载图片
// 加载本地图片
#define UIImageWithName(Name) ([UIImage imageNamed:Name])
// 通过路径加载本地图片
#define UIImageOfFile(Name) ([UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:Name ofType:nil]])


#pragma mark - device 设备

#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])

#pragma mark - screen 屏幕

#define iPhone4Or4S ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
#define iPhone5Or5s ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
#define iPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
#define iPhoneX \
({BOOL isPhoneX = NO;\
if (@available(iOS 11.0, *)) {\
isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;\
}\
(isPhoneX);})

#pragma mark - system 系统
//iOS版本号
#define iOS11OrLater ([[[UIDevice currentDevice] systemVersion]doubleValue]>=11.0f)
#define iOS10OrLater ([[[UIDevice currentDevice] systemVersion]doubleValue]>=10.0f)
#define iOS9OrLater ([[[UIDevice currentDevice] systemVersion]doubleValue]>=9.0f)
#define iOS8OrLater ([[[UIDevice currentDevice] systemVersion]doubleValue]>=8.0f)
#define iOS7OrLater ([[[UIDevice currentDevice] systemVersion]doubleValue]>=7.0f)

#pragma mark - color 颜色
//颜色相关
#define UIColorFromHexWithAlpha(hexValue,a) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:a]
#define UIColorFromHex(hexValue)            UIColorFromHexWithAlpha(hexValue,1.0)
#define UIColorFromRGBA(r,g,b,a)            [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define UIColorFromRGB(r,g,b)               UIColorFromRGBA(r,g,b,1.0)
#define UIColorFromRandom                   [UIColor colorWithRed:arc4random_uniform(255.0)/255.0 green:arc4random_uniform(255.0)/255.0 blue:arc4random_uniform(255.0)/255.0 alpha:1.0];

#define kBlackColor       [UIColor blackColor]
#define kDarkGrayColor    [UIColor darkGrayColor]
#define kLightGrayColor   [UIColor lightGrayColor]
#define kWhiteColor       [UIColor whiteColor]
#define kRedColor         [UIColor redColor]
#define kBlueColor        [UIColor blueColor]
#define kGreenColor       [UIColor greenColor]
#define kCyanColor        [UIColor cyanColor]
#define kYellowColor      [UIColor yellowColor]
#define kMagentaColor     [UIColor magentaColor]
#define kOrangeColor      [UIColor orangeColor]
#define kPurpleColor      [UIColor purpleColor]
#define kBrownColor       [UIColor brownColor]
#define kClearColor       [UIColor clearColor]


#pragma mark - indexPath

#define INDEX_PATH(a,b) [NSIndexPath indexPathWithIndexes:(NSUInteger[]){a,b} length:2]

#pragma mark - transforms 弧度转换

// 弧度转换为度。
#define kRadiansToDegrees(radians) ((radians) * (180.0 / M_PI))

// 度转换为randians。
#define kDegreesToRadians(angle) ((angle) / 180.0 * M_PI)

#pragma mark - other

#define kOnePixel 0.5

#pragma mark - 强弱引用
/*
 弱引用/强引用  可配对引用在外面用WeakSelf(self)，block用StrongSelf(self)
 也可以单独引用在外面用ICWeakSelf(self) block里面用weakself
 */
#define Weakify(type)  __weak typeof(type) weak##type = type;
#define Strongify(type)  __strong typeof(type) type = weak##type;

#define WeakSelf  Weakify(self);
#define StrongSelf  Strongify(self);

#pragma mark - Device Frame 屏幕相关的frame

#define kScreenScale ([UIScreen mainScreen].scale)
// 获取屏幕的宽度
#define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
// 获取屏幕的高度
#define kScreenHeight ([UIScreen mainScreen].bounds.size.height)

//字体适配(目前统一使用系统默认字体)
#define AdaptedFontSize(R)     [UIFont systemFontOfSize:AdaptedWidth(R)]
#define AdaptedBoldFontSize(R)  [UIFont boldSystemFontOfSize:AdaptedWidth(R)]

//不同屏幕尺寸适配（414，736是因为效果图为iPhone6plus如果不是则根据实际情况修改）
#define kScreenWidthRatio  (kScreenWidth / 414.0)
#define kScreenHeightRatio (kScreenHeight / 736.0)
#define AdaptedWidth(x)  ceilf((x) * (IS_IPAD ? 1 : kScreenWidthRatio))
#define AdaptedHeight(x) ceilf((x) * (IS_IPAD ? 1 : kScreenHeightRatio))

#define kStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
#define kTabbarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?83:49)
#define kNavBarHeight (kStatusBarHeight + 44)

#define AdaptedScreenBottom(x) iPhoneX ? ceilf((x - 34) * kScreenWidthRatio) : ceilf((x) * kScreenWidthRatio)

#pragma mark - static method 公用方法

#define NSStringFormat(f, ...)      [NSString stringWithFormat:f, ## __VA_ARGS__]
#define StringEncoding(string) [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]] ///<去除含中文的url
#define URLString(string) [NSURL URLWithString:StringEncoding(string)]

// 简化通知
#define kPostNotificationWithName(notificationName) \
[kNotificationCenter postNotificationName:notificationName object:nil userInfo:nil]

// 简化通知
#define kPostNotificationWithNameAndUserInfo(notificationName, userInfo) \
[kNotificationCenter postNotificationName:notificationName object:nil userInfo:userInfo]

#pragma mark - 线程

#define dispatch_sync_main_safe(block)\
if ([NSThread isMainThread]) {\
block();\
} else {\
dispatch_sync(dispatch_get_main_queue(), block);\
}

#define dispatch_async_main_safe(block)\
if ([NSThread isMainThread]) {\
block();\
} else {\
dispatch_async(dispatch_get_main_queue(), block);\
}

// 获取全局队列
#define kGlobalThread dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

#pragma mark - Judge 判断

// 判断当前定位是否为横向。
#define kIsLandscape (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))

// 判断它是否是空字符串。
#define kIsEmptyString(s) (s == nil || [s isKindOfClass:[NSNull class]] || ([s isKindOfClass:[NSString class]] && s.length == 0))

// 判断它是否为nil或null对象。
#define kIsEmptyObject(obj) (obj == nil || [obj isKindOfClass:[NSNull class]])

// 判断它是否是一个有效的字典。
#define kIsDictionary(objDict) (objDict != nil && [objDict isKindOfClass:[NSDictionary class]])

// 判断它是否是一个有效的数组。
#define kIsArray(objArray) (objArray != nil && [objArray isKindOfClass:[NSArray class]])


#define isKindClass(class1,class2)  [class1 isKindOfClass:NSClassFromString(class2)]            ///< 判断是否为同一个类
#define noNillString(object)  IsEmpty(object) ? @"" : object                                    ///< 返回一个不为Null的字符串
#define ifElse(class1,class2)  IsEmpty(class1) ? class2 : class1                                ///< 简便的双目运算符


#endif /* CDYMacro_h */
